<?
echo $nntp_server;
if ($sub=="on")
{
	$group_list = $sg_c;
	$bcount     = true; 
}
else if ($find_name!="")
{
	$group_list = $nntp->get_group_list("*" . $find_name . "*");  
} 
		
		
if ($sub=="all"&&$find_name!="")
{



 ?>

<table>
	   <tr>
		  <td>
			  <img src="images/logo.gif">
		  </td>
		  <td nowrap>
			  <table>
				<tr>
				   <td nowrap id="td_search_options">
				 <a class='normal'
				 <? if ($sub=="on") { ?>
				   href="newsgroups.php?sub="
				 <? } else { ?>
				   style="color:black;font:bold"
				 <? } ?>
				 >All Groups</a>&nbsp;&nbsp;&nbsp;
				 <a class='normal'
				 <? if ($sub=="") { ?>
				   href="newsgroups.php?sub=on"
				 <? } else { ?>
				   style="color:black;font:bold"
				 <? } ?>
				 >Subscribed Groups</a>&nbsp;&nbsp;&nbsp;
				 Messages&nbsp;&nbsp;&nbsp;
				 Message
				   </td>
				</tr>
				<tr>
				<form action="newsgroups.php" method="get">
				   <td nowrap id="td_search_panel"> 
						<input type="text" size=40 name="f" value='<?= $find_name ?>' >
						<input type="hidden" name="g"  value='<?= $group_name ?>' >
						<input type="hidden" name="sub"  value='<?= $sub ?>' >
						<a href="newsgroups.php?a=option">Server settings</a>
						<input type="submit" value="Search">  
						<input type="button" value="Reset" onclick="fb_edit('f', '')">
					</td>
				</form>
				</tr>
			  </table> 
		  </td>
		  <td nowrap>
			  <div id="dupdate"></div>
		  </td>
	   </tr>
	</table> <?
}



$limit = 60;
print '<div class="resizeXY" id="dmessagedetail"  style="display:none"></div>'; 
if (strlen($super_search) > 0)
{

	if (!(isset($pp_searchall) && is_array($pp_searchall)))
	{
		print "You must select at least one group to search.";
	}
	print '<div class="resizeXY" id="dmessagelist" >';
	$exec_begin = exact_millisec() ; 
	foreach ($pp_searchall as $c)
	{ 
	
		if ($nntp->join_group($c))
		{
		   
			
				print "<script language='javascript'>
				dupdate.innerHTML = 'searching " . $c . "...';
				 </script>";       
				 
			$s = $nntp->search_newsgroup (1, "*" . $super_search . "*");
	  
	   
			 if (count ($s["articles"]) > 0)
			 {
				 print "<a href='newsgroups.php?fi=" . $super_search ."&g=" . $c ."'>" . $c ."</a><br>";
			 }
				 
			 foreach ($s["articles"] as $a)
			 {
			 
			  $exec_end = exact_millisec() ;  
			  $span=floor($exec_end-$exec_begin);
				print "<script language='javascript'>
				dupdate.innerHTML = \"<font color=gray>" . $c ."</font><br>" . htmlentities(trim($a->subject)). "<br>" . $span ."\";
				 </script>";     
	
				print "....<a class='navig' href='javascript:search_load_image(\"" . $a->nntp_message_id . "\",\"" . $c . "\")'>" . $a->subject . "</a><br>";
			 }
			 if (count ($s["articles"]) > 0)
			 {
				 print "<hr>";
			 }
			 
			  $exec_end = exact_millisec() ;  
			  $span=$exec_end-$exec_begin;
			  if ($exec_end-$exec_begin > $limit)
			  {
				print "Search timed out";
				break;
			  }
			  
		} 
	} 
	print "<script language='javascript'>
	dupdate.innerHTML = 'Done';
	 </script>";     
	 
	print '</div>'; 
}
else
{
if ($find_name!="")
{ ?>  
	<div style="width:100%;border-top:solid 1px #339999;background-color:#CCFFCC;padding:3px">
	<table width="100%" cellpadding="1" cellspacing="1">
	  <tr>
		 <td><div id="dpages">
		 <?= count ($group_list) ?> groups
		 
		 <?
		 if ($sub=="on")
		 {
			print " To search ALL selected groups, enter text below and click Search  ";
		 }
		 else
		 {
			print " To search for newsgroups, enter text above and click Search  ";
		 }
		 
	if (count ($group_list) == 0 && $find_name!="")
	{
			print "Search is complete. There are no results to display.  ";
	}
	if (count ($group_list) > RESULT_PAGE_SIZE)
	{ 	 
		$max_pages  = floor ( (count ($group_list) / RESULT_PAGE_SIZE) + 0.5);
		$first_page = floor($start_at / 10) * 10;                                                                                                          
		$last_page  = $first_page + 10; 
		if ($max_pages < $last_page)
		{
		   $last_page = $max_pages;
		}
		 
		if ($first_page > 0)
		{
			print "<a class='normal' href='newsgroups.php?s=" . ($first_page-1) . "'>&laquo; Previous</a> | ";
		}
		else
		{
			print "&laquo; | ";
		}
		
		for ($x=$first_page;$x<$last_page;$x++) 
		{ 
			if ( $start_at==$x ) 
			{ 
				print "<b>" . ($x+1) . "</b> | "; 
			} 
			else 
			{ 
				print "<a class='normal' href='newsgroups.php?f=" . $find_name . "&s=" . $x . "'>" . ($x+1) . "</a> | "; 
			}                                                                                                         
		} 
		
		if ($x<$last_page)
		{
			print "<a class='normal' href='newsgroups.php?f=" . $find_name . "&s=" . $x . "'>Next &raquo;</a>"; 
		} 
		else
		{
			print "&raquo;";
		}
	}
		 
		 ?>
		 
		 </div></td>
		 <td align="right"><div id="dmetrics">
		  
		 </div></td>
	  </tr>
	</table>
	</div>  
<? } ?>


<form action="newsgroups.php?f=<?= $_GET["f"]?>&s=<?= $start_at ?>&sub=<?= $sub ?>" method="get">

 
 <?
 /*
 
 ,
		 "alt.binaries.pictures.movie-posters", 
		 "alt.binaries.celebrities",  
		 "alt.binaries.pictures.aviation" , 
		 "alt.binaries.sounds.mp3.books"
		 "alt.binaries.pictures.nature",   
		 "alt.binaries.pictures.art.pin-up",  
		 "alt.binaries.photography",     
		 "alt.binaries.models.repost",   
		 "alt.binaries.pictures.anime",    
 */
 if ($find_name==""&&$sub=="")
 {
 $py=150;
 print "<div align=center><a href='newsgroups.php?a=option' style=color:gray>".$nntp_server."</a></div>";

	 if (isset($sg_c) && is_array($sg_c) && count($sg_c) > 0)
	 {
	     $preview_cache=array();
		 foreach ($sg_c as $tmp)
		 {
		    if (trim($tmp)!="")
			{
			     $group_info = $nntp->join_group($tmp); 
				 if ($group_info!=NULL)
				 {
		            $preview_cache[]=$tmp;
				 }
			}
		 }
		 for ($e=count($preview_cache)-1;$e<count($preview_temp);$e++)
		 {
		     $preview_cache[]=$preview_temp[$e];
		 }
		  $preview_temp = $preview_cache;
	 }
	 
	 $preview_groups = array();
	 $preview_index  = array();
	 $offset = time() % count ($preview_temp);
	 for ($e=0;$e<count($preview_temp);$e++)
	 { 
		 $preview_index[count($preview_index)] = 1; 
	     $preview_groups[count($preview_groups)] = $preview_temp[$offset];
		 $offset=$offset<count($preview_temp)-1?$offset+1:0; 
	 }  
  ?> 
		  
<table border='0' cellpadding="0" cellspacing="1" width="100%" height="85%"><tr><td align="center">
	<table border='0' background="" cellpadding="0" cellspacing="1" style="border:solid 1px navy"  bgcolor="#6699FF" align="center">
		<tr>
		
               <td colspan="5" valign='top' nowrap 
			   style="FILTER: dropshadow(OffX=1, OffY=1,Color='#000000', Positive='true');color:white;background-color:slateblue;padding:4px;border-bottom:solid 1px white "> 
		   <img border='0' src="images/logo.gif" align='left'>
                  <b style="color:white;font-size:16pt;font-family:Arial, Helvetica, sans-serif">NZFree</b>  
                  Newsgroup browser<br><small><b>Anything you want</b></small>
               </td>  
		</tr> 
		 <tr>
		  
		 
		
		<!-- row 1 -->
		<tr>
		   <td align="center" colspan='2'> 
			  <div style="height:320;width:320;overflow:hidden">
			  <a href="newsgroups.php?g=<?= $preview_groups[0] ?>">
			   <img border='0' 
			        id=abe0 
					onerror="abe0.src='newsgroups.php?a=rand'" 
					src="newsgroups.php?message_id=X&gp=<?=$preview_index[0]?>&art_group=<?= $preview_groups[0] ?>&pp_w=320&pp_y=310&pp_x=6&pp_s=16"
					></a>
			  </div> 
		   </td>
		   
		   <td > 
				<table border='0' cellpadding="0" cellspacing="1">
					<tr>
					   <td colspan='2' align="center"> 
					  <div style="width:320;height:160;overflow:hidden">
					 <a href="newsgroups.php?g=<?= $preview_groups[1] ?>">
					 <img border='0' 
			              id=abe3   
						  onerror="abe3.src='newsgroups.php?a=rand'" 
						  src="newsgroups.php?message_id=X&gp=<?=$preview_index[1]?>&art_group=<?= $preview_groups[1] ?>&pp_w=321&pp_y=20&pp_x=6&pp_s=12"
						  ></a>
					  </div>   
					   </td>
					    
					</tr>
					
					
		<!-- row 2 -->
					<tr>
					   <td align="center">
						  <div style="width:160;height:160;overflow:hidden">
						  <a href="newsgroups.php?g=<?= $preview_groups[2] ?>">
						  <img border='0' 
			                   id=abe4  
						       onerror="abe4.src='newsgroups.php?a=rand'" 
							   src="newsgroups.php?message_id=X&gp=<?=$preview_index[2]?>&art_group=<?= $preview_groups[2] ?>&pp_a=90&pp_w=160&pp_y=<?=$py?>&pp_x=6&pp_s=9"
						  ></a>
						  </div>  
					   </td>
					   
					   <td align="center">
						  <div style="width:160;height:160;overflow:hidden">
						  <a href="newsgroups.php?g=<?= $preview_groups[3] ?>">
						  <img border='0' 
			                   id=abe1  
						       onerror="abe1.src='newsgroups.php?a=rand'" 
							   src="newsgroups.php?message_id=X&gp=<?=$preview_index[3]?>&art_group=<?= $preview_groups[3] ?>&pp_w=160&pp_y=<?=$py?>&pp_x=6&pp_s=9"
						  ></a>
						  </div> 
					   </td>
					   
					</tr> 
					
				</table> 
		   </td>
		   <td align="center">
			  <div style="width:160;height:320;overflow:hidden">
			  <a href="newsgroups.php?g=<?= $preview_groups[8] ?>">
			  <img border='0' 
				   id=abe8  
				   onerror="abe8.src='newsgroups.php?a=rand'" 
				   src="newsgroups.php?message_id=X&gp=<?=$preview_index[8]?>&art_group=<?= $preview_groups[8] ?>&pp_a=270&pp_w=322&pp_y=<?=$py?>&pp_x=150&pp_s=9"
			  ></a>
			  </div> 
		   </td> 
		</tr>
		
		
		<!-- row 3 -->
		<tr>
		   <td align="center"> 
			  <div style="width:160;height:160;overflow:hidden">
			  <a href="newsgroups.php?g=<?= $preview_groups[5] ?>">
			   <img border='0' 
			        id=abe6  
				    onerror="abe6.src='newsgroups.php?a=rand'" 
					src="newsgroups.php?message_id=X&gp=<?=$preview_index[5]?>&art_group=<?= $preview_groups[5] ?>&pp_w=160&pp_y=<?=$py?>&pp_x=6&pp_s=9"
						  ></a>
			  </div> 
		   </td>
		   <td align="center"> 
			  <div style="width:160;height:160;overflow:hidden">
			  <a href="newsgroups.php?g=<?= $preview_groups[6] ?>">
			   <img border='0' 
			        id=abe5 
				    onerror="abe5.src='newsgroups.php?a=rand'" 
					src="newsgroups.php?message_id=X&gp=<?=$preview_index[6]?>&art_group=<?= $preview_groups[6] ?>&pp_w=160&pp_y=<?=$py?>&pp_x=6&pp_s=9"
						  ></a>
			  </div> 
		   </td>
		   <td align="center" colspan=2>
				<table cellpadding="0" cellspacing="1">
				  <tr> 
				   <td align="center">
					  <div style="width:160;height:160;overflow:hidden">
					  <a href="newsgroups.php?g=<?= $preview_groups[9] ?>">
					  <img border='0' 
						   id=abe9  
				           onerror="abe9.src='newsgroups.php?a=rand'" 
						   src="newsgroups.php?message_id=X&gp=<?=$preview_index[9]?>&art_group=<?= $preview_groups[9] ?>&pp_w=160&pp_a=90&pp_y=<?=$py?>&pp_x=6&pp_s=9"
					  ></a>
					  </div> 
				   </td> 
				   <td align="center">
					  <div style="height:160;overflow:hidden">
					  <a href="newsgroups.php?g=<?= $preview_groups[4] ?>">
					   <img border='0' 
							id=abe2  width="322"
				            onerror="abe2.src='newsgroups.php?a=rand'" 
							src="newsgroups.php?message_id=X&gp=<?=$preview_index[4]?>&art_group=<?= $preview_groups[4] ?>&pp_w=322&pp_y=<?=$py?>&pp_x=6&pp_s=12"
								  ></a>
					  </div> 
				   </td> 
				  </tr>
				</table>
		   </td>
		</tr>
		
		<tr>
		   <td style="filter:progid:DXImageTransform.Microsoft.Gradient 
    (GradientType=1,StartColorStr='#ffffffff',EndColorStr='#6699FF');padding:3px" colspan="5" align="right" bgcolor="#FFFFFF">  
		  <a href="javascript:fb_edit ('sub','on')">Subscribed groups</a>
		  
		  &nbsp;&nbsp;&nbsp;
		  
		   <b>Find a newsgroup:</b>
		   <input type="text" value="" size="10" name="f">
						<input type="hidden" name="g"  value='<?= $group_name ?>' >
						<input type="hidden" name="sub"  value='<?= $sub ?>' >
		   <input type="submit" value="Search">
		   </td>
		</tr>
	</table>
	<!-- <?= $cookie_bookmark ?> -->
	<table>
	<tr>
	<?
	
	if ($cookie_bookmark!="")
	{
		$arr = explode ("&", $cookie_bookmark);
		foreach ($arr as $a)
		{
			$mark = explode (",", $a);
			print "<td><img src='newsgroups.php?message_id=".$mark[1]."&art_group=".$mark[0]."&pp_w=140'></td>"; 
		}
	}
	
	?>
	</tr>
	</table>
</td></tr></table>
<script language="javascript">
// document.body.bgColor = "whitesmoke";
var scrollers = new Array();
var loaders   = new Array();
var retries   = new Array();
var success   = new Array();
var scroller  = 0;
function rt (i)
{
   var x=new Image();
   success.push (0);
   retries.push (0);
   loaders.push (x);
   scrollers.push (document.getElementById(i));
}
function ex ()
{ 
	window.status = "Loading " + scrollers[scroller].src + "..."; 	
	scrollers[scroller].onload = function ()
	{
		this.style.display = "inline"
		success[scroller] = this.src;
		scroller = scroller<scrollers.length-1?scroller+1:0;
		window.setTimeout ("ex()", 10);
	}
	scrollers[scroller].onerror = function ()
	{
	   // this.style.display = "none"
		if (retries[scroller] < 5)
		{
		 //   this.style.display = "none";
		    window.status = "Retrying " + scroller + "...";
	        retries[scroller] ++;
	        this.src="newsgroups.php?a=rand";
		} 
		else
		{ 
		    retries[scroller] = 0;
			scroller = scroller<scrollers.length-1?scroller+1:0;
		    window.setTimeout ("ex()", 10);
		}
	}
	scrollers[scroller].src=scrollers[scroller].src; 
}
rt ('abe0');
rt ('abe1');
rt ('abe2');
rt ('abe3');
rt ('abe4');
rt ('abe5');
rt ('abe6'); 
rt ('abe8');
rt ('abe9');
//   ex();
window.onload = ex; //("ex()", 1000)
</script>
<? 
 
} 

else if ($sub=="on") {
    $checkbox_name = "searchall"; ?>
 
		<input type="text"  name="fe" value='<?= $super_search ?>' >
		<input type="hidden" name="g"  value='<?= $group_name ?>' >
		<input type="hidden" name="sub"  value='<?= $sub ?>' >
<input type="submit"  
 value="Search Groups"> 
 
 <? } else {
    $checkbox_name = "subscribe";?>
<input type="submit" 
 
 value="Subscribe"> to selected groups 
 <? }
 
 
print '
	   <table cellpadding=2 cellspacing=1>';  
if (!$bcount&&$find_name!="") 
{
	  $x = time() % count ($group_list);
	  $gl=$group_list[$x];
	  print "<tr>
			<td valign=top align=center>"; 
	  print "
		   <div align='center' id='dinfo' style='width:200;height:200;overflow:hidden;display:none;border:solid 1px gray'>
		   <a href='newsgroups.php?g=" . $gl . "'"   
			 . "><img onload=\"re('i".$x."',198)\" 
				onerror=\"MM_findObj('i".$x."').style.display='none';\" 
				src='newsgroups.php?message_id=X&gp=1&art_group=".$gl."'
				style='display:' id='i".$x."' border='0'></a>
				<br>
				".$gl."
		   </div>";
	 
	 print "</td>
			<td valign=top><div class='resizeXY'  >
			<table cellpadding=2 cellspacing=1><tr>";
}


$first_page = $start_at * RESULT_PAGE_SIZE;                                                                                                          
$last_page  = $first_page + RESULT_PAGE_SIZE;  
if (count ($group_list) < $last_page)
{
   $last_page = count ($group_list);
}
$imgcount=0;
sort ($group_list);

for ($x=$first_page;$x<$last_page;$x++)  
{ 
   $gl = $group_list[$x];
   if (strlen( trim( $gl ) ) > 0)
   {
		$cache_path = "data/" . $gl . ".DAT";
		if ( file_exists ($cache_path))			
		{ 
			$content = implode ('', file ($cache_path));
			parse_str ($content);
			$timestamp=date ("m/d/Y H:i:s", $timestamp);
			$first_art = $arr[0];
		} 
		else
		{
			$timestamp="";
			$first_art=1;
		}
		if ($bcount) 
		{ 
		
		    
			print "<td style='border:solid 1px whitesmoke' align=center>"; 
			$imgcount ++;
			if ($gi=$nntp->join_group($gl))
			{ 
			   print "
			   <div align='center' style='width:200;height:200;overflow:hidden'>
			   <a href='newsgroups.php?g=" . $gl . "'"   
			     . "><img onload=\"re('i".$x."',198)\" 
			        onerror=\"MM_findObj('i".$x."').style.display='none';\" 
					src='newsgroups.php?message_id=X&gp=".$first_art."&art_group=".$gl."'
			        style='display:' id='i".$x."' border='0'></a>
			   </div>"; 
			}
			else
			{
				#echo "<b>".$messages_ini["error"]["group_fail"].$gl." </b><br>"; 
				#echo $nntp->get_error_message()."<br>";  
			}         
			print "<div align='left'><input type=checkbox";
			if (array_search($gl,$sg_c)!==false)
			{
			   print " checked ";
			}
			print " name='" .  $checkbox_name ."[]' value='" . $gl . "'>";        
			print '<a class="normal" href="newsgroups.php?g=' . $gl . '"';  
			print ">" . $gl . "</a><br>"; 
			print "Articles: " . $gi["count"] . "<br>"; 
			print "Last Updated: " . $timestamp . "<br>"; 
			print "</div></td>";
			
			
			if ($imgcount%4==0)
			{
			    print "</tr><tr>";
			}
		}     
		else
		{
			print "<tr>"; 
			print "<td><input type=checkbox";
			if (array_search($gl,$sg_c)!==false)
			{
			   print " checked ";
			}
			print " name='" .  $checkbox_name ."[]' value='" . $gl . "'></td>";  
			print "<td>";      
			print '<a class="normal" href="newsgroups.php?g=' . $gl . '"';  
			print ">" . $gl . "</a>";    
			print "</td>";
			print "<td color=slateblue>" . $timestamp . "</td> ";
			print "</tr>";
		}     
	}
} 
if(!$bcount)
{
    print " </table></div> </td> </tr>";
}
print '</table>
       '; ?>
</form>	   

<? }


		$dir = CACHE_PATH . "asset";  
		$arr = array();
		if (is_dir($dir)) 
		{
		   if ($dh = opendir($dir)) 
		   {
			   while (($file = readdir($dh)) !== false) 
			   { 
				   if (preg_match ("/(.*)\.jpg/i", $file, $tmp))
				   {   
					   $diff = time() - filemtime($dir."/".$file);
					   if ($diff>14400)
					   {
					       unlink ($dir."/".$file);
					   } 
				   }
			   }
			   closedir($dh);
		   }
		} 


 ?>


